home *** CD-ROM | disk | FTP | other *** search
/ Pulps on CDRom / Pulps on CDROM.iso / comm.z / Utility.js < prev    next >
Text File  |  1998-10-20  |  3KB  |  209 lines

  1. /* ==================================================================
  2.  
  3. FILE:   Utility.js
  4.  
  5. DESCR:  Misc. global APIs.
  6.  
  7. NOTES:  
  8.  
  9. ================================================================== */
  10.  
  11.  
  12.  
  13. /*
  14.  
  15. DESCR:   Does non-case-sensitive comparison for equality of two URLS,
  16.  
  17.          using only the portion beginning with the filename.
  18.  
  19. PARAMS:  URL1_, URL2_  The URLs.
  20.  
  21. RETURNS: True if equal, false otherwise.
  22.  
  23. NOTES:   Not applicable to nethelp: URLs. Also requires that any fragment
  24.  
  25.          specifier attached to the URLs does not contain "/".
  26.  
  27. */
  28.  
  29. function URLsSansPathsAreSame( URL1_, URL2_ )
  30.  
  31. {
  32.  
  33.    // Standardize case.
  34.  
  35.    var URL1 = URL1_.toUpperCase()
  36.  
  37.    var URL2 = URL2_.toUpperCase()
  38.  
  39.  
  40.  
  41.    // Remove any path.
  42.  
  43.    URL1 = URL1.substring( URL1.lastIndexOf( "/" ) + 1 )
  44.  
  45.    URL2 = URL2.substring( URL2.lastIndexOf( "/" ) + 1 )
  46.  
  47.  
  48.  
  49.    // Compare.
  50.  
  51.    return ( URL1 == URL2 )
  52.  
  53. }
  54.  
  55.  
  56.  
  57. /*
  58.  
  59. DESCR:   Converts a URL to a simple filename (no path, no trailing
  60.  
  61.          specifiers).
  62.  
  63. PARAMS:  URL  The URL.
  64.  
  65. RETURNS: The converted URL.
  66.  
  67. NOTES:   Not applicable to nethelp: URLs. Also requires that any fragment
  68.  
  69.          specifier attached to the URLs does not contain "/".
  70.  
  71. */
  72.  
  73. function makeSimpleFilename( URL )
  74.  
  75. {
  76.  
  77.    var filename
  78.  
  79.  
  80.  
  81.    // Standardize case.
  82.  
  83.    filename = URL.toLowerCase()
  84.  
  85.  
  86.  
  87.    // Remove any path.
  88.  
  89.    filename = filename.substring( filename.lastIndexOf( "/" ) + 1 )
  90.  
  91.  
  92.  
  93.    // Remove any specifiers.
  94.  
  95.    if ( filename.indexOf( "#" ) > 0 ) {
  96.  
  97.        filename = filename.substring( 0, filename.indexOf( "#" ) )
  98.  
  99.    }
  100.  
  101.    if ( filename.indexOf( "?" ) > 0 ) {
  102.  
  103.       filename = filename.substring( 0, filename.indexOf( "?" ) )
  104.  
  105.    }
  106.  
  107.  
  108.  
  109.    return filename
  110.  
  111. }
  112.  
  113.  
  114.  
  115. /*
  116.  
  117. DESCR:   Runtime error handler.
  118.  
  119. PARAMS:  Standard JS event params.
  120.  
  121. RETURNS: Boolean to optionally supress runtime error dialogs.
  122.  
  123. NOTES:   If ERRS_TO_CONSOLE is ture, sends JS runtime errors to console for
  124.  
  125.          any window that binds this handler. If ERR_DLGS is true, error
  126.  
  127.          dialogs will not be supressed.
  128.  
  129. */
  130.  
  131. function errHandler( msg, URL, lineNum )
  132.  
  133. {
  134.  
  135.    if ( ERRS_TO_CONSOLE ) {
  136.  
  137.  
  138.  
  139.       // Report error to console.
  140.  
  141.       java.lang.System.out.println( "" )
  142.  
  143.       java.lang.System.out.println( "Error:" )
  144.  
  145.       java.lang.System.out.println( msg )
  146.  
  147.       java.lang.System.out.println( URL )
  148.  
  149.       java.lang.System.out.println( lineNum )
  150.  
  151.       java.lang.System.out.println( "" )
  152.  
  153.    }
  154.  
  155.  
  156.  
  157.    // Allow or supress runtime error dialogs.
  158.  
  159.    return ( ERR_DLGS ? false : true )
  160.  
  161. }
  162.  
  163.  
  164.  
  165. /*
  166.  
  167. DESCR:   Assertion routine.
  168.  
  169. PARAMS:  bAssertion  Boolean expression.
  170.  
  171.          msg         Fail message.
  172.  
  173. RETURNS: 
  174.  
  175. NOTES:   Must define ASSERT = true in any module using this routine.
  176.  
  177. */
  178.  
  179. function assert( bAssertion, msg )
  180.  
  181. {
  182.  
  183.    if ( ASSERT && !bAssertion ) alert( "ASSERTION FAILED: " + msg )
  184.  
  185. }
  186.  
  187.  
  188.  
  189. /*
  190.  
  191. DESCR:   Determines if a variable has been defined.
  192.  
  193. PARAMS:  identifier  The identifier to test.
  194.  
  195. RETURNS: true if the identifier is defined; false otherwise.
  196.  
  197. NOTES:   
  198.  
  199. */
  200.  
  201. function defined( identifier )
  202.  
  203. {
  204.  
  205.    return ( typeof identifier == "undefined" ? false : true )
  206.  
  207. }
  208.  
  209.